using System;
using RedPoint.Runtime;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;

public class UIPointNode : MonoBehaviour, IPointerClickHandler
{
    public string fullPath;
    private Text txt_point;

    private void Awake()
    {
        txt_point = GetComponentInChildren<Text>();
    }

    private void Start()
    {
        PointNode pointNode = RedPointManager.Instance.AddListener(fullPath, OnChange);
        gameObject.name = pointNode.FullPath;
    }

    private void OnChange(int value)
    {
        txt_point.text = value.ToString();
    }

    public void OnPointerClick(PointerEventData eventData)
    {
        int value = RedPointManager.Instance.GetNodeValue(fullPath);

        if (eventData.button == PointerEventData.InputButton.Left)
        {
            RedPointManager.Instance.ChangeValue(fullPath, value + 1);
        }
        else if (eventData.button == PointerEventData.InputButton.Right)
        {
            RedPointManager.Instance.ChangeValue(fullPath, Mathf.Clamp(value - 1,0, value));
        }
    }
}
